﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using PCMSofCC.BLL;
using PCMSofCC;

namespace PCMSofCC.Member
{
    public partial class FillInRecord : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (PreviousPage != null && PreviousPage.IsCrossPagePostBack)
            {
                RadioButtonList rblDate = (RadioButtonList)PreviousPage.Master.FindControl("Main").FindControl("rblDate");
                DateTime date = DateHelper.GetMondayOfThisWeek(DateTime.Now.Date).AddDays(rblDate.SelectedIndex);
                ListBox SiteList = (ListBox)PreviousPage.Master.FindControl("Main").FindControl("SiteList");
                int siteId = int.Parse(SiteList.SelectedValue);
                SqlDataSource1.SelectParameters["SiteId"] = new Parameter("SiteId",TypeCode.Int32,siteId.ToString());
                SqlDataSource1.SelectParameters["Date"] = new Parameter("Date", TypeCode.DateTime, date.ToString());
            }
            else if (!IsPostBack)
                Response.Redirect("DisplayTask.aspx");
        }

        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            PCMSofCCDataContext db = new PCMSofCCDataContext();
            foreach (GridViewRow row in gvCompleteRecord.Rows)
            {
                RecordEntry entry = db.RecordEntry.Single(x => x.Id == (int)gvCompleteRecord.DataKeys[row.RowIndex].Value);
                entry.Score = int.Parse(((TextBox)row.FindControl("tbxScore")).Text);
                entry.Problem = ((TextBox)row.FindControl("tbxProblem")).Text;
                entry.Solution = ((TextBox)row.FindControl("tbxSolution")).Text;
            }
            db.SubmitChanges();
        }
    }
}
